package com.tgpms.util;

import com.tgpms.generator.plugins.Underline2Camel;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Common {

	private static String temp3 = "@[\\]\\[a-zA-Z0-9_.]+";
	private static String temp4 = "&[\\]\\[a-zA-Z0-9_.]+";

	/**
	 * 判断变量是否为空
	 *
	 * @param s
	 * @return null
	 */
	public static boolean isEmpty(Object s) {
		if (null == s || "".equals(s.toString()) ) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 使用率计算
	 *
	 * @return null
	 */
	public static String fromUsage(long free, long total) {
		Double d = new BigDecimal(free * 100 / total).setScale(1,
				BigDecimal.ROUND_HALF_UP).doubleValue();
		return String.valueOf(d);
	}
	
	
	/**
	 * 根据传入时间格式化当前时间
	 * @param pattern （yyyy-MM-dd HH:mm:ss）
	 * @return
	 */
	public static String nowTimeFomat(String pattern) {
		LocalDateTime time=LocalDateTime.now();
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
		return dtf.format(time);
	}

	/**
	 * 返回当前时间　格式：yyyy-MM-dd hh:mm:ss
	 *
	 * @return null String
	 */
	@Deprecated
	public static String fromDateH() {
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return format1.format(new Date());
	}

	/**
	 * 返回当前时间　格式：yyyy-MM-dd
	 *
	 * @return null String
	 */
	@Deprecated
	public static String fromDateY() {
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		return format1.format(new Date());
	}
	
	/**
	 * 返回当前时间格式字符串：yyyyMMddHHmmssSSS
	 *
	 * @return String
	 */
	@Deprecated
	public static String fromDateFormat() {
		DateFormat format1 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return format1.format(new Date());
	}
	
	/**
	 * 返回当前时间格式字符串：yyyyMMddHHmmss
	 *
	 * @return String
	 */
	@Deprecated
	public static String fromDateFormats() {
		DateFormat format1 = new SimpleDateFormat("yyyyMMddHHmmss");
		return format1.format(new Date());
	}
	
	/**
	 * 根据传入时间格式 返回当前时间格式字符串
	 * @param dateString 
	 * @return String
	 */
	@Deprecated
	public static String fromDateFormat(String dateString) {
		DateFormat format1 = new SimpleDateFormat(dateString);
		return format1.format(new Date());
	}

	/**
	 * 用来去掉List中空值和相同项的。
	 *
	 * @param list
	 * @return null
	 */
	public static List<String> removeSameItem(List<String> list) {
		List<String> difList = new ArrayList<String>();
		for (String t : list) {
			if (t != null && !difList.contains(t)) {
				difList.add(t);
			}
		}
		return difList;
	}

	/**
	 * 返回用户的IP地址
	 *
	 * @param request
	 * @return null
	 */
	public static String toIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	/**
	 * 传入原图名称，，获得一个以时间格式的新名称
	 *
	 * @param fileName
	 *            　原图名称
	 * @return null
	 */
	public static String generateFileName(String fileName) {
		DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		String formatDate = format.format(new Date());
		int random = new Random().nextInt(10000);
		int position = fileName.lastIndexOf(".");
		String extension = fileName.substring(position);
		return formatDate + random + extension;
	}

	/**
	 * 取得html网页内容 UTF8编码
	 *
	 * @param urlStr
	 *            网络地址
	 * @return null String
	 */
	public static String getInputHtmlUTF8(String urlStr) {
		URL url = null;
		try {
			url = new URL(urlStr);
			HttpURLConnection httpsURLConnection = (HttpURLConnection) url
					.openConnection();

			httpsURLConnection.setRequestMethod("GET");
			httpsURLConnection.setConnectTimeout(5 * 1000);
			httpsURLConnection.connect();
			if (httpsURLConnection.getResponseCode() == 200) {
				// 通过输入流获取网络图片
				InputStream inputStream = httpsURLConnection.getInputStream();
				String data = readHtml(inputStream, "UTF-8");
				inputStream.close();
				return data;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return null;

	}

	/**
	 * 取得html网页内容 GBK编码
	 *
	 * @param urlStr
	 *            网络地址
	 * @return null String
	 */
	public static String getInputHtmlGBK(String urlStr) {
		URL url = null;
		try {
			url = new URL(urlStr);
			HttpURLConnection httpsURLConnection = (HttpURLConnection) url
					.openConnection();

			httpsURLConnection.setRequestMethod("GET");
			httpsURLConnection.setConnectTimeout(5 * 1000);
			httpsURLConnection.connect();
			if (httpsURLConnection.getResponseCode() == 200) {
				// 通过输入流获取网络图片
				InputStream inputStream = httpsURLConnection.getInputStream();
				String data = readHtml(inputStream, "GBK");
				inputStream.close();
				return data;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

		return null;

	}

	/**
	 * @param inputStream
	 * @param uncode
	 *            编码 GBK 或 UTF-8
	 * @return null
	 * @throws Exception
	 */
	public static String readHtml(InputStream inputStream, String uncode)
			throws Exception {
		InputStreamReader input = new InputStreamReader(inputStream, uncode);
		BufferedReader bufReader = new BufferedReader(input);
		String line = "";
		StringBuilder contentBuf = new StringBuilder();
		while ((line = bufReader.readLine()) != null) {
			contentBuf.append(line);
		}
		return contentBuf.toString();
	}

	/**
	 *
	 * @return null 返回资源的二进制数据 @
	 */
	public static byte[] readInputStream(InputStream inputStream) {

		// 定义一个输出流向内存输出数据
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		// 定义一个缓冲区
		byte[] buffer = new byte[1024];
		// 读取数据长度
		int len = 0;
		// 当取得完数据后会返回一个-1
		try {
			while ((len = inputStream.read(buffer)) != -1) {
				// 把缓冲区的数据 写到输出流里面
				byteArrayOutputStream.write(buffer, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				byteArrayOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		}

		// 得到数据后返回
		return byteArrayOutputStream.toByteArray();

	}

	/**
	 * 获取当前认证通过的用户名
	 *
	 * @return null
	 */
	public static String getAuthenticatedUsername() {
		String username = null;
		Object principal = SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		if (principal instanceof UserDetails) {
			username = ((UserDetails) principal).getUsername();
		} else {
			username = principal.toString();
		}
		return username;
	}

	public static String ParseExpression(Map infoMap, String strPassedParam)
			throws Exception {
		String strRel = strPassedParam;
		Pattern p = Pattern.compile(temp3);
		Matcher m = p.matcher(strPassedParam);
		while (m.find()) {
			String strMColumn = m.group(0).toString();
			String strColumn = strMColumn.substring(1); // 除去"@"
			if (infoMap.containsKey(strColumn.toUpperCase())) {
				String strValue = infoMap.get(strColumn.toUpperCase())
						.toString(); // 查找出对应数值
				// ，
				// 进行转换
				strRel = strRel.replaceAll(strMColumn, strValue);
			}
		}
		return strRel;
	}

	public static String ParseExpression2(Map infoMap, String strPassedParam)
			throws Exception {
		String strRel = strPassedParam;
		Pattern p = Pattern.compile(temp4);
		Matcher m = p.matcher(strPassedParam);
		while (m.find()) {
			String strMColumn = m.group(0).toString();
			String strColumn = strMColumn.substring(1); // 除去"@"
			if (infoMap.containsKey(strColumn.toUpperCase())) {
				String strValue = infoMap.get(strColumn.toUpperCase())
						.toString(); // 查找出对应数值
				// ，
				// 进行转换
				strRel = strRel.replaceAll(strMColumn, strValue);
			}
		}
		return strRel;
	}
	
	
	
	/**
     * 动态sql处理参数方法
     *
     * @param map
     * @return
     */
    public static Map<String, String> fixSql(Map<String, String> map) {
        Map<String, String> maps = new HashMap<>();
        String sql = map.get("sql").toUpperCase();
        map.remove("sql");
        if (null == sql) {
            return null;
        }

        for (String param : map.keySet()) {
            String getParams = "@" + Underline2Camel.underline2Camel(param, true);
            char[] chars = getParams.toCharArray();
            chars[1] += 32;
            sql = sql.replaceAll(String.valueOf(chars).toUpperCase(), map.get(param).toUpperCase());
        }
        maps.put("sql", sql);
        return maps;
    }
    
    
    /**
     * 动态sql处理逗号参数拼接组装map
     *
     * @return
     */
    public static List<Map<String, String>> StringToMap(String key,String desc){
    	List<Map<String, String>> resList = new ArrayList<Map<String, String>>();

    	try {
			String[] keys = key.split(",");
			String[] descs = desc.split(",");
			for(int i=0;i<keys.length;i++) {
				Map<String, String> resMap = new HashMap<String, String>();
				resMap.put("id", keys[i]);
				resMap.put("name", descs[i]);
				resList.add(resMap);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return resList;
		}
		return resList;
    	
    }
    
    
    
    /**
    * @Author newway
    * @Description 针对字符串str右补s,i为s的个数
    * @Date 2019年8月23日下午5:12:25
    * @Param 
    * @return 
    **/
    public static String rightSupplement(String str,String s,Integer Int) {
    	for(int i=1;i<=Int;i++) {
    		str = str+s;
    	}
    	return str;
    }
    
    
    /**获取指定长度随机数
     * @param length
     * @return
     */
    public static String getRamdonString(Integer length) {
    	StringBuffer sb = new StringBuffer();
        String ALLCHAR = "0123456789";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
        }
        return sb.toString();
    }
    
    public static String getUTFStringByEncoding(String str) {
		String encode = "UTF-8";
		String returnStr = "";
		try {
			if(str!=null){
				if (str.equals(new String(str.getBytes("GB2312"), "GB2312"))) {
					encode = "GB2312";
				}else if (str.equals(new String(str.getBytes("ISO-8859-1"), "ISO-8859-1"))) {
					encode = "ISO-8859-1";
				}else if (str.equals(new String(str.getBytes("UTF-8"), "UTF-8"))) {
					encode = "UTF-8";
				}else if (str.equals(new String(str.getBytes("GBK"), "GBK"))) {
					encode = "GBK";
				}
				if(encode.equals("UTF-8")){
					returnStr = str;
				}else{
					returnStr = new String(str.getBytes(encode),"UTF-8");
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return returnStr;
	}

	
}

